package tcp;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class DateServer {
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		try {
			// 端口的范围在 0 - 65535
			// 服务器不用指定ip地址
			ServerSocket sock = new ServerSocket(9000);

			// 监听连接
			while (true) {
				System.out.println("waiting for connection ...");
				Socket client = sock.accept(); // 阻塞，accept返回客户机socket接口
				System.out.println("accepted");

				PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
				pout.println(new Date().toString()); // 将日期内容写入socket中

				client.close(); // 关闭socket，等待下一次连接
			}
		} catch (IOException ioe) {
			System.out.println(ioe);
		}
	}
}